import com.sun.source.tree.Tree;

public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode creatTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        B.left = D;
        B.right = E;
        E.right = H;
        A.right = C;
        C.left = F;
        C.right = G;
        return A;
    }

   //前序遍历
    public void preOder(TreeNode root){
        if(root == null){
            return ;
        }
        System.out.print(root.val +" ");
        preOder(root.left);
        preOder(root.right);
    }

    //中序遍历
    public void inOder(TreeNode root){
        if(root == null){
            return ;
        }
        inOder(root.left);
        System.out.print(root.val + " ");
        inOder(root.right);
    }

    //后序遍历
    public void postOder(TreeNode root){
        if(root == null){
            return ;
        }
        postOder(root.left);
        postOder(root.right);
        System.out.println(root.val + " ");
    }

    //通过遍历方式
    public static int countSize = 0;
    public void size(TreeNode root){
        if(root == null){
            return ;
        }
        size(root.left);
        size(root.right);
        countSize++;
    }

    public int nodeSize(TreeNode root){
        if(root == null){
            return 0;
        }
        return nodeSize(root.left) + nodeSize(root.right) + 1;
    }


    //通过遍历的方法
    public static int leaveCount = 0;
    public void leaveSize(TreeNode root){
        if(root == null){
            return ;
        }
        if(root.left == null && root.right == null){
            leaveCount++;
        }
        leaveSize(root.left);
        leaveSize(root.right);
    }

    public int getLeaveCount(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        return getLeaveCount(root.left) + getLeaveCount(root.right) ;
    }

    public int getLever(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }
        return getLever(root.left,k -1 ) + getLever(root.right,k - 1);
    }

    public int getHigh(TreeNode root){
        if(root == null){
            return 0;
        }
        int ret1 = getHigh(root.left);
        int ret2 = getHigh(root.right);
        return Math.max(ret1,ret2) + 1;
    }

    public TreeNode Find(TreeNode root,char val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        TreeNode node1 = Find(root.left,val);
        if(node1 != null){
            return node1;
        }
        TreeNode node2 = Find(root.right,val);
        if(node2 != null){
            return node2;
        }
        return null;
    }
}
